//
// Created by xm on 2024/5/13.
//

#include "MyArray.h"
#include "iostream"
#include "sstream"

namespace Test04 {

    MyArray::MyArray(){
        this->mCapacity = 10;
        this->mSize = 0;
        this->pArray = new int[this->mCapacity];
        for (int i = 0; i < this->mCapacity; ++i) {
            this->pArray[i] = 0;
        }
    }
    MyArray::MyArray(int capacity, int val){
        this->mCapacity = capacity;
        this->mSize = 0;
        this->pArray = new int[this->mCapacity];
        for (int i = 0; i < this->mCapacity; ++i) {
            this->pArray[i] = val;
        }
    }
    MyArray::~MyArray()
    {
        if(this->pArray != NULL){
            delete[] this->pArray;
            this->pArray= NULL;
        }
    }
    MyArray::MyArray(const MyArray &arr)
    {
        this->mCapacity = arr.mCapacity;
        this->mSize = arr.mSize;
        // 申请空间
        this->pArray = new int[arr.mCapacity];
        // 拷贝数据
        for (int i = 0; i < this->mCapacity; ++i) {
            this->pArray[i] = arr.pArray[i];
        }

    }
    MyArray& MyArray::operator=(const MyArray &m)
    {
        std::cout << "=重载 " << m.description() << std::endl;

        // 释放原来的空间
        if(this->pArray != NULL){
            delete[] this->pArray;
            this->pArray = NULL;
        }

        this->mCapacity = m.mCapacity;
        this->mSize = m.mSize;
        // 申请空间
        this->pArray = new int[m.mCapacity];
        // 拷贝数据
        for (int i = 0; i < this->mCapacity; ++i) {
            this->pArray[i] = m.pArray[i];
        }

        return *this;
    }
    // 重写数组下标运算符，即当左值，又当右值
    int& MyArray::operator[](int index)
    {
//        std::cout << "[]重载 " << index << std::endl;
        if(this->mSize <= index){
            this->mSize++;
        }


        return this->pArray[index];
    }


    // 头插
    void MyArray::pushFront(int val)
    {
        if(this->mSize == this->mCapacity){
            return;
        }

        for (int i = 0; i < this->mSize; ++i) {
            this->pArray[i+i] = this->pArray[i];
        }
        this->pArray[0] = val;
        this->mSize++;
    }

    // 尾插
    void MyArray::pushBack(int val)
    {
        if(this->mSize == this->mCapacity){
            return;
        }
        this->pArray[this->mSize] = val;
        this->mSize++;
    }
    // 头删
    void MyArray::popFront()
    {
        if(this->mSize==0){
            return;
        }

        for (int i = 0; i < this->mSize-1; i++)
        {
            this->pArray[i] = (this->pArray[i+1]);
        }
        this->mSize--;

    }
    // 尾删
    void MyArray::popBack()
    {
        if(this->mSize==0){
            return;
        }
        this->mSize--;

    }
    // 获取元素个数
    int MyArray::size(){

        return this->mSize;
    }
    // 容量
    int MyArray::capacity(){
        return this->mCapacity;
    }
    // 指定位置掺入
    void MyArray::insert(int pos, int val)
    {
        // 没空间了
        if(this->mSize == this->mCapacity){
            return;
        }
        if(pos > this->mCapacity-1){
            return;
        }
        // 超出当前size
        if(this->mSize-1 < pos){
            pos = this->mSize;
            this->mSize++;
        } else{
            this->mSize ++;
        }
        for (int i = pos; i < this->mSize-1; ++i) {
            this->pArray[i+1] = this->pArray[i];
        }
        this->pArray[pos] = val;
    }
    // 获取指定位置的值
    int &MyArray::get(int pos){
        return this->pArray[pos];
    }
    // 在指定位置修改值
    void MyArray::set(int pos, int val)
    {
        if(pos <0 || pos > this->mCapacity-1){
            return;
        }
        this->pArray[pos] = val;
    }

    std::string MyArray::description() const
    {
        std::stringstream addr;
        addr << this;

        std::string message =  "NyArray<" + addr.str() + "> " + "size:" + std::to_string(this->mSize) + " capacity:" +std::to_string(
                this->mCapacity);

        return message;
    }

    void MyArray::toPrint() {


        std::cout << this->description()  << "\n";
        for (int i = 0; i < this->mSize; ++i) {
            std::cout << "'" << i << "':" << this->pArray[i] << " ";
        }
        std::cout << std::endl;
    }


} // Test04